home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / attrib.cmm < prev    next >
Encoding:
Text File  |  1995-10-05  |  7.1 KB  |  231 lines

  1. /*
  2.  * Attrib.cmm
  3.  *
  4.  * CMM Attrib. This program allows you to view or set file attribute bits.
  5.  *
  6.  * Runs on any version of CEnvi
  7.  */
  8.  
  9. #include "Netware.lib"
  10.  
  11. /* ---------------------------------------------------------------------
  12.    The following variables allow us to keep track of which bits the user
  13.    wants us to twiddle.
  14.    --------------------------------------------------------------------- */
  15.  
  16. #define BITS "RHSEDA"
  17.  
  18. flags = 0;          // 1 if user wants to change the values
  19. flagor = 0;         // stores the +A, etc options
  20. flagand = 0;        // stores the -A, etc options
  21.  
  22.  
  23. /* ---------------------------------------------------------------------
  24.    These variables tell us what command line options the user selected
  25.    --------------------------------------------------------------------- */
  26.  
  27. filename = "";      // filename to be worked on - can have wildcards
  28. recursive = 0;      // should we do recursive stuff, like *.cmm in all
  29.                     // subdirectories
  30. help = 0;           // If so, we print the usage display
  31.  
  32. /*
  33.  * A standard usage help display.
  34.  */
  35. usage()
  36. {
  37.   printf("Displays or changes file attributes.\n\n");
  38.   printf("ATTRIB [+ | -] bit ... [[drive:][path]filename] [/S]\n");
  39.   printf("\n  +   Sets an attribute.\n");
  40.   printf("  -   Clears an attribute.\n");
  41.   printf("  R   Read-only file attribute.\n");
  42.   printf("  A   Archive file attribute.\n");
  43.   printf("  S   System file attribute.\n");
  44.   printf("  H   Hidden file attribute.\n");
  45.   printf("  E   Executable file attribute.\n");
  46.   printf("  /S  Processes files in all directories in the specified path.\n");
  47. }
  48.  
  49. /*
  50.  * Examine the command line and set appropriate flags
  51.  */
  52. parse_args(argc,argv)
  53. {
  54.    for( i=1;i<argc;i++ )
  55.    {
  56.       if( argv[i][0]=='/' )
  57.       {
  58.          switch( argv[i][1] )
  59.          {
  60.             case 's': recursive = 1; break;
  61.             case '?': help = 1; break;
  62.             default: usage();
  63.          }
  64.          continue;
  65.       }
  66.  
  67.       if( argv[i][0]=='-' || argv[i][0]=='+' )
  68.       {
  69.          chr = toupper(argv[i][1]); search = "1"; search[0] = chr;
  70.          if( chr=='D' )
  71.          {
  72.             printf("You may not set or clear the directory bit.\n");
  73.             exit(EXIT_FAILURE);
  74.          }
  75.          if( (bit = strcspn(BITS,search))==strlen(BITS) )
  76.          {
  77.             printf("Unrecognized atribute: %c\n",argv[i][1]);
  78.             exit(EXIT_FAILURE);
  79.          }
  80.  
  81.          if( argv[i][0]=='+' )
  82.          {
  83.             flagor |= (1<<bit);
  84.          } else {
  85.             // If you turn on read-only, netware automatically sets these bits. So,
  86.             // if you turn it off, we should remove them
  87.             if( defined(_NWNLM_) && (chr=='R') )
  88.                flagand |= _A_NODELET | _A_NORENAM;
  89.  
  90.             flagand |= (1<<bit);
  91.          }
  92.          flags = 1;
  93.          continue;
  94.       }
  95.       if( strlen(filename)>0 )
  96.       {
  97.          printf("Too many files on command line.\n");
  98.          exit(EXIT_FAILURE);
  99.       }
  100.       filename = argv[i];
  101.    }
  102. }
  103.  
  104.  
  105. /*
  106.  * This is a Netware function. It won't work on the other systems.
  107.  */
  108. DOSTimeFromCalendar(time)
  109. {
  110.    undefine(field1);
  111.    undefine(field2);
  112.    NLMLink("_ConvertTimeToDOS",time,field1,field2);
  113.    return (field2<<16) | field1;
  114. }
  115.  
  116. /*
  117.  * This is a DOS/Windows only function
  118.  */
  119. SetFileAttributes(pFileName,pAttributes)
  120. {
  121.    lReg.ah = 0x43;
  122.    lReg.al = 1;
  123.    lReg.cx = pAttributes;
  124.    if( !defined(_DOS32_) )  {
  125.       lReg.ds = segment(pFileName); lReg.dx = offset(pFileName);
  126.    } else {
  127.       lReg.dx = pointer(pFileName);
  128.    }
  129.    return interrupt(0x21,lReg);
  130. }
  131. /*
  132.  * We process the arguments, then do the work. This should act as an exact
  133.  * clone of the DOS attrib command.
  134.  */
  135. main(argc,argv)
  136. {
  137.    parse_args(argc,argv);
  138.  
  139.    if( help ) usage();
  140.  
  141. // Get an array of the files we are to work with.
  142.  
  143.    if( strlen(filename)==0 ) filename = "*.*";
  144.    files = Directory(filename,recursive,~_A_SUBDIR);
  145.    if( files==NULL ) {
  146.       printf("File not found - %s\n",filename);
  147.       exit(EXIT_FAILURE);
  148.    }
  149.    number = GetArraySpan(files)+1;
  150.  
  151.  
  152.    // In this case, the user just wants us to list the atributes for the
  153.    // specified filespec.
  154.  
  155.    if( flags==0 )
  156.    {
  157.       for( i=0;i<number;i++ )
  158.         if( defined(_NWNLM_) )
  159.           printf(" %s %s %s %s %s %s %s %s %s %s %s     %s\n",
  160.                  (files[i].attrib & _A_SUBDIR)?"D":"-",
  161.                  (files[i].attrib & _A_RDONLY)?"RO":"RW",
  162.                  (files[i].attrib & _A_SHARE)?"SH":"--",
  163.                  (files[i].attrib & _A_HIDDEN)?"H":"-",
  164.                  (files[i].attrib & _A_SYSTEM)?"SY":"--",
  165.                  (files[i].attrib & _A_TRANS)?"T":"-",
  166.                  (files[i].attrib & _A_IMMPURG)?"P":"-",
  167.                  (files[i].attrib & _A_ARCH)?"A":"-",
  168.                  (files[i].attrib & _A_NOCOPY)?"CI":"--",
  169.                  (files[i].attrib & _A_NODELET)?"DI":"--",
  170.                  (files[i].attrib & _A_NORENAM)?"RI":"--",
  171.                  FullPath(files[i].name));
  172.         else
  173.           printf("  %c%c%c%c%c%c   %s\n",
  174.                  (files[i].attrib & _A_ARCH)?'A':' ',
  175.                  (files[i].attrib & _A_SUBDIR)?'D':' ',
  176.                  (files[i].attrib & _A_EXECUTE)?'E':' ',
  177.                  (files[i].attrib & _A_SYSTEM)?'S':' ',
  178.                  (files[i].attrib & _A_HIDDEN)?'H':' ',
  179.                  (files[i].attrib & _A_RDONLY)?'R':' ',
  180.                  FullPath(files[i].name));
  181.       exit(EXIT_SUCCESS);
  182.     }
  183.  
  184.  
  185.   // At this point, he doesn't want a listing of those files, he wants to
  186.   // do some changes to them.
  187.  
  188.   for( i=0;i<number;i++ )
  189.     {
  190.       if( defined(_NWNLM_) )
  191.         {
  192. // For this to work, we must tell Netware to change the bits for file
  193. // 'files[i].name' to the value (file[i].attrib &flagand)|flagor.
  194.  
  195.           tempcreate[0] = DOSTimeFromCalendar(files[i].Create);
  196.           tempaccess[0] = DOSTimeFromCalendar(files[i].Access);
  197.           tempdate[0] =   DOSTimeFromCalendar(files[i].Write);
  198.           tempbackup[0] = DOSTimeFromCalendar(files[i].Backup);
  199.  
  200.           code = NLMLink("SetFileInfo",
  201.                          files[i].name,
  202.                          0x06,
  203.                          (files[i].attrib & ~flagand) | flagor,
  204.                          tempcreate,
  205.                          tempaccess,
  206.                          tempdate,
  207.                          tempbackup,
  208.                          files[i].uid);
  209.           if( code )
  210.             printf("\nError setting attributes for file %s\n",file[i].name);
  211.           continue;
  212.         }
  213.       if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
  214.         {
  215.           SetFileAttributes(files[i].name,
  216.                             (files[i].attrib & ~flagand) | flagor);
  217.           continue;
  218.         }
  219.       if( defined(_NTCON_) || defined(_NTWIN_) )
  220.         {
  221.           DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,files[i].name,
  222.                       (files[i].attrib & ~flagand) | flagor);
  223.           continue;
  224.         }
  225.  
  226.       printf("I do not know how to set file attributes on this system.\n");
  227.       exit(EXIT_FAILURE);
  228.     }
  229.   exit(EXIT_SUCCESS);
  230. }
  231.